/*
 * TITLE:Game
 * AUTHOR:Manori Thakur
 * PURPOSE: Is triggered after successful login, launches the game world
 */
package com.example.android.debuggermenu;

import java.util.HashMap;
import java.util.List;

import clientcore.GameState;
import clientcore.Question;
import clientresponse.ResponseQuestionLog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;

public class Game extends DebuggerActivity {
    private static final String TAG = Game.class.getSimpleName();
    private GameView gameView = null;
    public static double scale;
    
    private Intent item1Intent = null, //Creates new Intents to be passed to new activities on menu item select
	   item2Intent = null, 
	   item3Intent = null, 
	   item4Intent = null, 
	   item5Intent = null;
     

     
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
                
        scale=1;
        // requesting to turn the title OFF
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
        
	    // making it full screen
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        gameView = new GameView(this);
        RelativeLayout relativeLayout = new RelativeLayout(this); 
        setContentView(relativeLayout);
        relativeLayout.addView(gameView);        
        
        ZoomControls zoomControls = new ZoomControls(this); 
        relativeLayout.addView(zoomControls);
        zoomControls.setGravity(Gravity.TOP | Gravity.CENTER);
        gameView.setEnabled(true);
        zoomControls.setIsZoomInEnabled(true);
        zoomControls.setIsZoomOutEnabled(true);
        zoomControls.setVisibility(View.VISIBLE);
        
        zoomControls.setOnZoomInClickListener(new OnClickListener() {

            public void onClick(View v) {
            	    gameView.invalidate();
	                scale *= 1.25;
            	
            }
        });
        zoomControls.setOnZoomOutClickListener(new OnClickListener() {

            public void onClick(View v) {
                gameView.invalidate();
                scale *= 0.8;
            }
        });
        //Uncomment the below statement if you dont want orientation changes
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Log.d(TAG, "View added");
    }
    
    @Override
    public void onPause(){
    	Log.d(TAG, "Pausing...");
    	/*
    	if(gameView!=null)
    		gameView.unregisterAccelerometer();
    	*/
    	gameView.getThread().setRunning(false);
    	boolean retry = true;
    	while (retry) {
    		try {
    			gameView.getThread().join();
    			retry = false;
    			Log.d(TAG, "Ended the thread");
    		} catch (InterruptedException e) {
    			// try again shutting down the thread
    		}
    	}
        super.onPause();
    }
    
    @Override
    public void onResume() {
    	Log.d(TAG, "Resuming...");
    	/*
    	if(gameView!=null)
    		gameView.registerAccelerometer();
    	*/
        super.onResume();
    }
        
    @Override
    public void onDestroy() {
        Log.d(TAG, "Destroying...");
        //gameView.unregisterAccelerometer();
        super.onDestroy();
    }

    @Override
    public void onStop() {
        Log.d(TAG, "Stopping...");
        /*
        if(gameView!=null)
        	gameView.unregisterAccelerometer();
        */
        super.onStop();
    }
    
  //VINCE: Sets up the menu screen when menu is pressed during the game
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.ingame_menu, menu);
    	return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	case R.id.friends:
    		item1Intent = new Intent(this, Friends.class);
    		item.setIntent(item1Intent);
    		startActivity(item1Intent);
    		return true;
    	case R.id.chat:
    		item2Intent = new Intent(this, Chat.class);
    		item.setIntent(item2Intent);
    		startActivity(item2Intent);
    		return true;
    	case R.id.questionlog:
    		/*BRIAN an example of retrieving question log
    		GameState.updateQuestionLog();
            List<Question> questionLog = GameState.getQuestionLog();
            if(questionLog.size() <= 0) {
            	Log.d(TAG,"No questions in log.");
            }
            else {
    		    for(Question question : questionLog) {
    		    	Log.d(TAG, question.getText());
    		    	Log.d(TAG, "You answered: " + (question.getStatus()? "Right" : "Wrong"));
    		    }
            }
            */
            
    		item3Intent = new Intent(this, QuestionLog.class);
    		item.setIntent(item3Intent);
    		gameView.getThread().setPaused(true);
    		startActivity(item3Intent);
    		return true;
            
    	case R.id.shop:
    		return true;
    	case R.id.logout:
    		AlertDialog.Builder logoutBuilder = new AlertDialog.Builder(this);
    		logoutBuilder.setMessage("Are you sure you want to logout?")
    			   .setCancelable(false)
    			   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    				   public void onClick(DialogInterface dialog, int id) {
    					   GameState.disconnect(); 
    					   Game.this.finish();
    		           }
    		       })
    		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
    		    	   public void onClick(DialogInterface dialog, int id) {
    		    		    dialog.cancel();
    		    	   }
    		       });
    		AlertDialog logoutAlert = logoutBuilder.show();
    		return true;
    	default:
    		return super.onOptionsItemSelected(item);
    	}
    }
    
}
